﻿using DynamicData;
using PosClient.Controls;
using PosClient.Event;
using PosClient.Model;
using PosClient.Service;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Splat;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Windows.Forms;
using System.Data;
using System.Threading.Tasks;
using PosClient.Helper;
using System.Dynamic;

namespace PosClient.ViewModel
{
    public class PayViewModel: ReactiveObject, IActivatableViewModel
    {
        public IScheduler _mainScheduler;

        public ViewModelActivator Activator { get; }

        #region 属性
        /// <summary>
        /// 支付方式列表
        /// </summary>
        public BindingList<Pay> PayList
        {
            get => _PayList;
            set => this.RaiseAndSetIfChanged(ref _PayList, value);
        }
        private BindingList<Pay> _PayList = new BindingList<Pay>()
        {
           new Pay()
           {
                PayCode = "2",
                PayName = "微信支付宝",
                PayType = 0,
                IsRefund = 1,
                IsRound = 0,
                IsVip = 1
            },
            new Pay()
            {
                PayCode = "1",
                PayName = "现金",
                PayType = 1,
                IsRefund = 1,
                IsRound = 1,
                IsVip = 1
            },
            new Pay()
            {
                PayCode = "3",
                PayName = "余额",
                PayType = 2,
                IsRefund = 1,
                IsRound = 0,
                IsVip = 0
            },
            new Pay()
            {
                PayCode = "4",
                PayName = "优惠券",
                PayType = 3,
                IsRefund = 1,
                IsRound = 0,
                IsVip = 1
            },
            new Pay()
            {
                PayCode = "5",
                PayName = "积分支付",
                PayType = 4,
                IsRefund = 1,
                IsRound = 0,
                IsVip = 1
            },
            new Pay()
            {
                PayCode = "6",
                PayName = "卡支付",
                PayType = 5,
                IsRefund = 1,
                IsRound = 0,
                IsVip = 0
            },
            new Pay()
            {
                PayCode = "7",
                PayName = "云闪付",
                PayType = 6,
                IsRefund = 1,
                IsRound = 0,
                IsVip = 1
            }
        };

        /// <summary>
        /// 当前支付方式
        /// </summary>
        public Pay SelectPay
        {
            get => _SelectPay;
            set => this.RaiseAndSetIfChanged(ref _SelectPay, value);
        }
        private Pay _SelectPay;
        
        /// <summary>
        /// 换购数据
        /// </summary>
        public BindingList<PromoteEventItem> ExChangeGoodList
        {
            get => _ExChangeGoodList;
            set => this.RaiseAndSetIfChanged(ref _ExChangeGoodList, value);
        }
        private BindingList<PromoteEventItem> _ExChangeGoodList = new BindingList<PromoteEventItem>();

        /// <summary>
        /// 换购购物车
        /// </summary>
        public List<Plu> ExchangeCart
        {
            get => _ExchangeCart;
            set => this.RaiseAndSetIfChanged(ref _ExchangeCart, value);
        }
        private List<Plu> _ExchangeCart = new List<Plu>();

        /// <summary>
        /// 索引
        /// </summary>
        public int Index
        {
            get => _Index;
            set => this.RaiseAndSetIfChanged(ref _Index, value);
        }
        private int _Index;

        /// <summary>
        /// 输入框金额
        /// </summary>
        public string PayAmountString
        {
            get => _PayAmountString;
            set => this.RaiseAndSetIfChanged(ref _PayAmountString, string.Format("{0:F2}", value));
        }
        private string _PayAmountString;

        /// <summary>
        /// 支付金额
        /// </summary>
        public decimal PayAmount
        {
            get => _PayAmount;
            set => this.RaiseAndSetIfChanged(ref _PayAmount, value);
        }
        private decimal _PayAmount;

        /// <summary>
        /// 支付总额
        /// </summary>
        public decimal TotalAmount
        {
            get => _TotalAmount;
            set => this.RaiseAndSetIfChanged(ref _TotalAmount, value);
        }
        private decimal _TotalAmount;

        /// <summary>
        /// 优惠总额
        /// </summary>
        public decimal TotalDiscount
        {
            get => _TotalDiscount;
            set => this.RaiseAndSetIfChanged(ref _TotalDiscount, value);
        }
        private decimal _TotalDiscount;

        /// <summary>
        /// 总数
        /// </summary>
        public decimal TotalCount
        {
            get => _TotalCount;
            set => this.RaiseAndSetIfChanged(ref _TotalCount, value);
        }
        private decimal _TotalCount;

        /// <summary>
        /// 未付金额
        /// </summary>
        public decimal RemainPayAmount
        {
            get => _RemainPayAmount;
            set => this.RaiseAndSetIfChanged(ref _RemainPayAmount, value);
        }
        private decimal _RemainPayAmount;

        /// <summary>
        /// 已付
        /// </summary>
        public decimal HasPayedAmount
        {
            get => _HasPayedAmount;
            set => this.RaiseAndSetIfChanged(ref _HasPayedAmount, value);
        }
        private decimal _HasPayedAmount;

        /// <summary>
        /// 找零金额
        /// </summary>
        public decimal ExchangeAmount
        {
            get => _ExchangeAmount;
            set => this.RaiseAndSetIfChanged(ref _ExchangeAmount, value);
        }
        private decimal _ExchangeAmount;

        /// <summary>
        /// 抹零金额
        /// </summary>
        public decimal RoundAmount
        {
            get => _RoundAmount;
            set => this.RaiseAndSetIfChanged(ref _RoundAmount, value);
        }
        private decimal _RoundAmount;

        /// <summary>
        /// 汇总主标题
        /// </summary>
        public string PayTitle
        {
            get => _PayTitle;
            set => this.RaiseAndSetIfChanged(ref _PayTitle, value);
        }
        private string _PayTitle;

        /// <summary>
        /// 汇总副标题
        /// </summary>
        public string PaySubTitle
        {
            get => _PaySubTitle;
            set => this.RaiseAndSetIfChanged(ref _PaySubTitle, value);
        }
        private string _PaySubTitle;

        /// <summary>
        /// 已支付列表
        /// </summary>
        public Stack<OrderPay> HasPayList
        {
            get => _HasPayList;
            set => this.RaiseAndSetIfChanged(ref _HasPayList, value);
        }
        private Stack<OrderPay> _HasPayList = new Stack<OrderPay>();



        public MainViewModel ParentViewModel { get; set; }

        public bool IsRefund
        {
            get => ParentViewModel.SaleMode == MainViewModel.SaleModeEnum.AfterSale;
        }
#endregion

        #region Command

        /// <summary>
        /// 确认支付
        /// </summary>
        public ReactiveCommand<Unit, Result<dynamic>> ConfirmCommand { get; }

        /// <summary>
        /// 索引变动
        /// </summary>
        public ReactiveCommand<Unit, Unit> IndexChangeCommand { get; }

        /// <summary>
        /// 缓存换购数据
        /// </summary>
        public ReactiveCommand<Unit, Unit> CacheExchangeGoodCommand { get; }

        /// <summary>
        /// 打开换购页面
        /// </summary>
        public ReactiveCommand<Unit, Unit> OpenExchangeGoodCommand { get; }


        public ReactiveCommand<Unit, Unit> ResetCommand { get; }

        /// <summary>
        /// 促销
        /// </summary>
        public ReactiveCommand<Unit, Unit> CalculateCommand { get; }

        #endregion

        public PayViewModel(IScheduler mainScheduler)
        {
            _mainScheduler = mainScheduler;
            Activator = new ViewModelActivator();
            ParentViewModel = Locator.Current.GetService<MainViewModel>();

            this.WhenAnyValue(vm => vm.Index).Do(y =>
            {
                if (y > -1 && y < PayList.Count)
                {
                    SelectPay = PayList[y];
                }
            }).Subscribe();

            //总额变动
            this.WhenAnyValue(x => x.TotalAmount)
                .Subscribe(x =>
                {
                    PayAmount = x;
                    RemainPayAmount = TotalAmount - HasPayedAmount;
                    TitleConverter(ParentViewModel.PluList.Sum(y => y.Count), x, TotalDiscount);
                });
            //主总额变动支付页面总额联动
            this.WhenAnyValue(x => x.ParentViewModel.TotalAmount)
                .Subscribe(x =>
                {
                    TotalAmount = x;
                });
            //已付变动
            this.WhenAnyValue(x => x.HasPayedAmount)
                .Subscribe(x =>
                {
                    RemainPayAmount = TotalAmount - x;
                    SubTitleConverter(HasPayedAmount, RemainPayAmount);
                });
            //未付变动
            this.WhenAnyValue(x => x.RemainPayAmount)
                .Subscribe(x =>
                {
                    PayAmountString = string.Format("{0:F2}", x);
                    ParentViewModel.RemainPayAmount = x;
                    SubTitleConverter(HasPayedAmount, RemainPayAmount);
                });
            //支付金额变动
            this.WhenAnyValue(x => x.PayAmount)
                .Subscribe(x =>
                {
                    PayAmountString = string.Format("{0:F2}", x);
                });
            //找零变动
            this.WhenAnyValue(x => x.ExchangeAmount)
              .Subscribe(x =>
              {
                  ParentViewModel.ExchangeAmount = x;
              });

            var canConfirm = this.WhenAnyValue(x => x.PayAmount, (x) => x > 0);

            ConfirmCommand = ReactiveCommand.Create(ConfirmCommandImpl,canConfirm,RxApp.MainThreadScheduler);
            IndexChangeCommand = ReactiveCommand.Create(IndexChangeCommandImpl);
            CacheExchangeGoodCommand = ReactiveCommand.Create(CacheExchangeGoodCommandImpl);
            OpenExchangeGoodCommand = ReactiveCommand.Create(OpenExchangeGoodCommandImpl);
            ResetCommand = ReactiveCommand.Create(ResetCommandImpl);
            CalculateCommand = ReactiveCommand.Create(CalculateCommandImpl);
        }

        /// <summary>
        /// 汇总标题转换
        /// </summary>
        /// <param name="value1">总数</param>
        /// <param name="value2">总额</param>
        /// <param name="value3">总优惠</param>
        private void TitleConverter(decimal value1, decimal value2, decimal value3)
        {
            PayTitle = string.Format("共{0}件商品，应付{1}元，合计优惠{2}元", value1, value2, value3);
        }
        /// <summary>
        /// 汇总副标题
        /// </summary>
        /// <param name="value1">已付</param>
        /// <param name="value2">待付</param>
        private void SubTitleConverter(decimal value1, decimal value2)
        {
            PaySubTitle = string.Format("已付{0}元，剩余{1}元待付", value1, value2);
        }

        private void IndexChangeCommandImpl()
        {
            if (SelectPay != null)
            {
                VipDiscountCommandImpl(SelectPay.IsVip == 0);
                if (SelectPay.IsRound == 1)
                {
                    decimal finalRemainPayAmount = Math.Floor(RemainPayAmount * 10) / 10;
                    //decimal finalRemainPayAmount = Math.Round(RemainPayAmount, 1, MidpointRounding.AwayFromZero);
                    PayAmount = finalRemainPayAmount;
                    PayAmountString = string.Format("{0:F2}", PayAmount);
                    RoundAmount = finalRemainPayAmount - RemainPayAmount;

                    RemainPayAmount = finalRemainPayAmount;
                }
                else
                {
                    RoundAmount = 0m;
                    PayAmount = ParentViewModel.TotalAmount - HasPayedAmount;
                    RemainPayAmount = PayAmount + RoundAmount;
                    PayAmountString = string.Format("{0:F2}", PayAmount + RoundAmount);
                }
            }
        }
        public void AddMoney(decimal money)
        {
            HasPayedAmount += money;
            RemainPayAmount = TotalAmount - HasPayedAmount + RoundAmount;
        }

        private void RollBack(OrderPay pay)
        {
            // 回退
            HasPayList.Pop();
            RemainPayAmount += pay.PayAmount;
            HasPayedAmount -= pay.PayAmount;
            RemainPayAmount += pay.PayAmount;
            //MessageBus.Current.SendMessage(new AddPayEventArgs { Pays = HasPayList },"AddPay");
        }

        private Result<dynamic> ConfirmCommandImpl()
        {
            Result<dynamic> result = new Result<dynamic>
            {
                Success = true,
                Message = "结账成功"
            };
            bool success = decimal.TryParse(PayAmountString, out var payAmount);
            if (!success)
            {
                result.Success = success;
                result.Message = "请输入正确的金额";
                return result;
            }
            PayAmount = payAmount;
            // 检查金额是否异常
            if (PayAmount >= 100000 || PayAmount < 0)
            {
                result.Success = false;
                result.Message = "异常金额";
                return result;
            }
            switch (SelectPay.PayType)
            {
                case 1:
                    {
                        AddMoney(PayAmount);
                        // 检查之前是否已经支付过
                        OrderPay pay = HasPayList.Where(x => x.PayType == SelectPay.PayType).FirstOrDefault();
                        if (pay == null)
                        {
                            pay = new OrderPay
                            {
                                PayAmount = PayAmount,
                                PayType = SelectPay.PayType,
                                PayName = SelectPay.PayName,
                                PayCode = SelectPay.PayCode,
                                CreateTime = DateTime.Now
                            };
                            HasPayList.Push(pay);
                            Logger.AuditLogger.Audit($"现金收款{PayAmount}");
                        }
                        else
                        {
                            pay.PayAmount += PayAmount;
                        }
                        // 现金
                        if ((RemainPayAmount + RoundAmount) <= 0)
                        {
                            pay.ExchangeAmount = RemainPayAmount;
                            ExchangeAmount += pay.ExchangeAmount;
                            Result<dynamic> results = new Result<dynamic>
                            {
                                Success = true,
                                Message = "结账成功"
                            };
                            results = CreateOrderCommandImpl(null);
                            if (!results.Success)
                            {
                                RollBack(HasPayList.Peek());
                            }
                            result.Data = pay;
                            MessageBus.Current.SendMessage(new SettlePayEventArgs { Result = results });
                        }
                        else
                        {
                            MessageBus.Current.SendMessage(new AddPayEventArgs() { Pays = HasPayList });
                        }
                        //设置数据为空，不进入支付确认页
                        result.Data = null;
                        break;
                    }
            }
            return result;
        }

        private void VipDiscountCommandImpl(bool discount)
        {
            if (discount)
            {
                var item = ParentViewModel.PluList.Where(x => x.Tag == Plu.PluTag.Vip).ToList();
                item.ForEach(x =>
                {
                    x.Tag = Plu.PluTag.Normal;
                    x.VipPrice = 0.00m;
                    x.Summary();
                });
                ParentViewModel.SummaryCommand.Execute().Subscribe();
            }
            else
            {
                ParentViewModel.VipDiscountCommand.Execute().Subscribe();
            }
        }


        private Result<dynamic> CreateOrderCommandImpl(dynamic arg)
        {
            Result<dynamic> result = new Result<dynamic>();
            result.Message = "结账成功";
            result.Success = true;
            using (var conn = FreeClient.Sqlite)
            {
                dynamic data = new ExpandoObject();
                Order order = new Order();
                order.OrderNo = ParentViewModel.OrderNo;
                order.StoreCode = Globals.StoreCode;
                order.PosNo = Globals.Pos;
                order.WorkNo = ParentViewModel.WorkNo;
                order.OriginAmount = ParentViewModel.PluList.Sum(x => x.OriginAmount);
                order.Amount = ParentViewModel.PluList.Sum(x => x.Amount);
                order.RoundAmount = RoundAmount;
                order.Username = Globals.Username;
                order.PayAmount = HasPayList.Sum(x => PayAmount);
                order.CreateTime = DateTime.Now;
                order.FlagRefund = (int)ParentViewModel.SaleMode;
                order.FlagSend = 0;
                order.BalanceDate = Globals.BalanceDate;
                order.DiscountAmount = order.OriginAmount - order.Amount;
                order.Id = conn.Insert(order).ExecuteInserted().FirstOrDefault().Id;
                data.Order = order;
                var items = new List<OrderItem>();
                foreach(var item in ParentViewModel.PluList)
                {
                    var orderItem = new OrderItem()
                    {
                        OrderNo = order.OrderNo,
                        OrderId = order.Id,
                        GoodId = ParentViewModel.PluList.IndexOf(item),
                        GoodCode = item.PluCode,
                        GoodName = item.PluName,
                        BarCode = item.Barcode,
                        OriginAmount = item.OriginAmount,
                        OriginPrice = item.OriginPrice,
                        Price = item.Price,
                        VipPrice = item.VipPrice,
                        Quantity = item.Count,
                        CreateTime = DateTime.Now,
                        Tag = (int)item.Tag,
                        GoodType = item.PluType,
                        Amount = item.Amount,
                        DiscountAmount = item.OriginAmount - item.Amount,
                        Promote1 = item.Promote1,
                        Promote2 = item.Promote2,
                        Promote3 = item.Promote3,
                        Promote4 = item.Promote4,
                        PromotePrice1 = item.PromotePrice1,
                        PromotePrice2 = item.PromotePrice2,
                        PromotePrice3 = item.PromotePrice3,
                        PromotePrice4 = item.PromotePrice4,
                    };
                    conn.Insert(orderItem).ExecuteInserted();
                    items.Add(orderItem);
                }
                data.Items = items;
                var pays = new List<OrderPay>();
                foreach(var pay in HasPayList)
                {
                    var orderPay = new OrderPay()
                    {
                        OrderId = order.Id,
                        OrderNo = order.OrderNo,
                        OriginAmount = order.OriginAmount,
                        PayCode = pay.PayCode,
                        PayName = pay.PayName,
                        PayNo = pay.PayNo,
                        PayAmount = pay.PayAmount,
                        ExchangeAmount = pay.ExchangeAmount,
                        TransNo = pay.TransNo,
                        PayType = pay.PayType,
                        OpenId = pay.OpenId,
                        CreateTime = DateTime.Now,
                        RequestNo = pay.RequestNo
                    };
                    conn.Insert(orderPay).ExecuteAffrows();
                    pays.Add(orderPay);
                }
                data.Pays = pays;
                result.data = data;
            }
            ParentViewModel.ClearCartCommand.Execute(false).Subscribe();
            ParentViewModel.OrderNo = OrderService.GetCurrentOrderNo();
            return result;
        }

        private void CacheExchangeGoodCommandImpl()
        {
            ExChangeGoodList.Add(new PromoteEventItem() 
            {
                ItemCode = "0002816",
                ItemName = "嘉德橄榄番茄大蒜味黄波",
                ItemValue = "19",
                MainPic = "<svg height=\"100\" node-id=\"1\" sillyvg=\"true\" template-height=\"100\" template-width=\"100\" version=\"1.1\" viewBox=\"0 0 100 100\" width=\"100\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs node-id=\"917\"><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_151_\" node-id=\"844\" spreadMethod=\"pad\" x1=\"58.0978\" x2=\"58.1635\" y1=\"44.7995\" y2=\"62.6342\"><stop offset=\"0\" stop-color=\"#fffff8\"/><stop offset=\"0.9486\" stop-color=\"#fffbe3\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_152_\" node-id=\"848\" spreadMethod=\"pad\" x1=\"79.0717\" x2=\"78.9958\" y1=\"42.6926\" y2=\"85.7649\"><stop offset=\"0\" stop-color=\"#ebf6fd\"/><stop offset=\"0.7916\" stop-color=\"#c4dff8\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_153_\" node-id=\"852\" spreadMethod=\"pad\" x1=\"61.2715\" x2=\"61.2763\" y1=\"30.4902\" y2=\"44.4551\"><stop offset=\"0.1304\" stop-color=\"#b7dff9\"/><stop offset=\"0.6595\" stop-color=\"#95c5ec\"/><stop offset=\"0.8424\" stop-color=\"#88bbe7\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_154_\" node-id=\"857\" spreadMethod=\"pad\" x1=\"64.4871\" x2=\"64.628\" y1=\"25.4482\" y2=\"15.8301\"><stop offset=\"0\" stop-color=\"#cbe3f9\"/><stop offset=\"0.7916\" stop-color=\"#e9f4fd\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_157_\" node-id=\"867\" spreadMethod=\"pad\" x1=\"53.6808\" x2=\"53.6808\" y1=\"47.447\" y2=\"86.977\"><stop offset=\"0.1304\" stop-color=\"#97c7ec\"/><stop offset=\"0.7425\" stop-color=\"#7aafe1\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_160_\" node-id=\"877\" spreadMethod=\"pad\" x1=\"55.3621\" x2=\"55.3621\" y1=\"21.5554\" y2=\"13.4795\"><stop offset=\"0\" stop-color=\"#72a9de\"/><stop offset=\"0.5418\" stop-color=\"#6da6dd\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_161_\" node-id=\"881\" spreadMethod=\"pad\" x1=\"61.8731\" x2=\"61.8731\" y1=\"21.5554\" y2=\"13.4795\"><stop offset=\"0\" stop-color=\"#72a9de\"/><stop offset=\"0.5418\" stop-color=\"#6da6dd\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_162_\" node-id=\"885\" spreadMethod=\"pad\" x1=\"67.582\" x2=\"67.582\" y1=\"21.5554\" y2=\"13.4795\"><stop offset=\"0\" stop-color=\"#72a9de\"/><stop offset=\"0.5418\" stop-color=\"#6da6dd\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_163_\" node-id=\"889\" spreadMethod=\"pad\" x1=\"73.6919\" x2=\"73.6919\" y1=\"21.5554\" y2=\"13.4795\"><stop offset=\"0\" stop-color=\"#72a9de\"/><stop offset=\"0.5418\" stop-color=\"#6da6dd\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_164_\" node-id=\"893\" spreadMethod=\"pad\" x1=\"25.6422\" x2=\"25.6898\" y1=\"53.2609\" y2=\"86.6028\"><stop offset=\"0.1304\" stop-color=\"#fff0e6\"/><stop offset=\"0.4215\" stop-color=\"#ffdbc0\"/><stop offset=\"0.7425\" stop-color=\"#ffc291\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_165_\" node-id=\"898\" spreadMethod=\"pad\" x1=\"25.628\" x2=\"25.6995\" y1=\"42.7104\" y2=\"48.5795\"><stop offset=\"0.1304\" stop-color=\"#fff0e4\"/><stop offset=\"0.428\" stop-color=\"#ffdbbe\"/><stop offset=\"0.7425\" stop-color=\"#ffc291\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_166_\" node-id=\"903\" spreadMethod=\"pad\" x1=\"25.6878\" x2=\"25.6738\" y1=\"51.7196\" y2=\"48.9012\"><stop offset=\"0\" stop-color=\"#fffce5\"/><stop offset=\"0.4772\" stop-color=\"#fffdf1\"/><stop offset=\"1\" stop-color=\"#fffdf9\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_167_\" node-id=\"908\" spreadMethod=\"pad\" x1=\"76.6508\" x2=\"79.4813\" y1=\"29.5007\" y2=\"40.2463\"><stop offset=\"0.1657\" stop-color=\"#6ea7dd\"/><stop offset=\"0.7916\" stop-color=\"#76abdf\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_168_\" node-id=\"912\" spreadMethod=\"pad\" x1=\"25.6826\" x2=\"25.6847\" y1=\"73.7575\" y2=\"82.4364\"><stop offset=\"0.1719\" stop-color=\"#fffff7\"/><stop offset=\"0.9498\" stop-color=\"#fffbe3\"/></linearGradient><clipPath id=\"SVGID_156_\" node-id=\"864\"><path d=\"M 42.90 40.50 L 73.50 40.50 L 73.50 87.00 L 42.90 87.00 Z\" fill=\"#000000\" fill-rule=\"nonzero\" node-id=\"1012\" stroke=\"none\" target-height=\"46.5\" target-width=\"30.599998\" target-x=\"42.9\" target-y=\"40.5\"/></clipPath><clipPath id=\"SVGID_159_\" node-id=\"874\"><path d=\"M 75.70 15.00 L 77.30 15.30 L 78.60 16.20 L 79.40 17.40 L 79.70 19.00 L 79.70 26.50 L 49.30 26.50 L 49.30 19.00 L 49.60 17.40 L 50.50 16.20 L 51.80 15.30 L 53.30 15.00 L 75.70 15.00 Z\" fill=\"#000000\" fill-rule=\"nonzero\" node-id=\"1016\" stroke=\"none\" target-height=\"11.5\" target-width=\"30.399998\" target-x=\"49.3\" target-y=\"15\"/></clipPath></defs><path d=\"M 42.90 40.50 L 73.50 40.50 L 73.50 87.00 L 42.90 87.00 Z\" fill=\"url(#SVGID_151_)\" fill-rule=\"nonzero\" node-id=\"1022\" stroke=\"none\" target-height=\"46.5\" target-width=\"30.599998\" target-x=\"42.9\" target-y=\"40.5\"/><path d=\"M 84.60 82.90 L 73.50 87.00 L 73.50 40.60 L 84.60 36.60 Z\" fill=\"url(#SVGID_152_)\" fill-rule=\"nonzero\" node-id=\"1024\" stroke=\"none\" target-height=\"50.4\" target-width=\"11.099998\" target-x=\"73.5\" target-y=\"36.6\"/><path d=\"M 73.50 40.50 L 42.90 40.50 L 49.10 26.50 L 79.70 26.50 Z\" fill=\"url(#SVGID_153_)\" fill-rule=\"nonzero\" node-id=\"1026\" stroke=\"none\" target-height=\"14\" target-width=\"36.799995\" target-x=\"42.9\" target-y=\"26.5\"/><path d=\"M 75.70 15.00 L 77.30 15.30 L 78.60 16.20 L 79.40 17.40 L 79.70 19.00 L 79.70 26.50 L 49.30 26.50 L 49.30 19.00 L 49.60 17.40 L 50.50 16.20 L 51.80 15.30 L 53.30 15.00 L 75.70 15.00 Z\" fill=\"url(#SVGID_154_)\" fill-rule=\"nonzero\" node-id=\"1028\" stroke=\"none\" target-height=\"11.5\" target-width=\"30.399998\" target-x=\"49.3\" target-y=\"15\"/><g node-id=\"1062\"><g clip-path=\"url(#SVGID_156_)\" group-id=\"1\" node-id=\"1032\"><path d=\"M 73.50 50.00 L 73.50 87.00 L 34.50 86.60 L 33.90 50.60 L 41.90 50.60 L 41.90 66.80 L 42.10 67.80 L 42.70 68.60 L 43.80 69.20 L 46.40 69.10 L 47.80 67.80 L 48.00 55.40 L 48.20 54.40 L 49.50 53.00 L 50.50 52.80 L 51.50 53.00 L 52.90 54.40 L 53.10 62.80 L 53.30 63.80 L 54.60 65.10 L 55.60 65.40 L 56.60 65.10 L 58.00 63.80 L 58.20 59.30 L 58.90 57.50 L 60.70 56.80 L 62.50 57.50 L 63.30 59.30 L 63.50 71.00 L 64.80 72.30 L 65.80 72.50 L 67.60 71.80 L 68.40 70.00 L 68.60 49.00 L 69.90 47.70 L 70.90 47.50 L 72.70 48.20 L 73.50 50.00 Z\" fill=\"url(#SVGID_157_)\" fill-rule=\"nonzero\" group-id=\"1,3\" node-id=\"1034\" stroke=\"none\" target-height=\"39.5\" target-width=\"39.6\" target-x=\"33.9\" target-y=\"47.5\"/></g></g><g node-id=\"1063\"><g clip-path=\"url(#SVGID_159_)\" group-id=\"2\" node-id=\"1040\"><path d=\"M 55.40 15.00 L 55.40 20.10\" fill=\"none\" group-id=\"2,4\" node-id=\"1042\" stroke=\"url(#SVGID_160_)\" stroke-linecap=\"round\" stroke-width=\"3\" target-height=\"5.1000004\" target-width=\"0\" target-x=\"55.4\" target-y=\"15\"/><path d=\"M 61.90 15.00 L 61.90 20.10\" fill=\"none\" group-id=\"2,4\" node-id=\"1044\" stroke=\"url(#SVGID_161_)\" stroke-linecap=\"round\" stroke-width=\"3\" target-height=\"5.1000004\" target-width=\"0\" target-x=\"61.9\" target-y=\"15\"/><path d=\"M 67.60 15.00 L 67.60 20.10\" fill=\"none\" group-id=\"2,4\" node-id=\"1046\" stroke=\"url(#SVGID_162_)\" stroke-linecap=\"round\" stroke-width=\"3\" target-height=\"5.1000004\" target-width=\"0\" target-x=\"67.6\" target-y=\"15\"/><path d=\"M 73.70 15.00 L 73.70 20.10\" fill=\"none\" group-id=\"2,4\" node-id=\"1048\" stroke=\"url(#SVGID_163_)\" stroke-linecap=\"round\" stroke-width=\"3\" target-height=\"5.1000004\" target-width=\"0\" target-x=\"73.7\" target-y=\"15\"/></g></g><path d=\"M 34.50 86.60 L 16.80 86.60 L 14.70 86.00 L 13.20 84.40 L 12.60 82.30 L 13.10 73.50 L 17.60 53.30 L 33.80 53.30 L 38.30 73.50 L 38.80 82.30 L 38.60 83.40 L 37.50 85.30 L 35.70 86.40 L 34.50 86.60 Z\" fill=\"url(#SVGID_164_)\" fill-rule=\"nonzero\" node-id=\"1052\" stroke=\"none\" target-height=\"33.3\" target-width=\"26.199999\" target-x=\"12.6\" target-y=\"53.3\"/><path d=\"M 36.30 48.50 L 14.60 48.30 L 14.00 47.50 L 14.00 47.00 L 15.70 44.10 L 17.00 43.00 L 34.30 43.00 L 35.70 44.10 L 37.40 47.00 L 37.20 48.00 L 36.30 48.50 Z\" fill=\"url(#SVGID_165_)\" fill-rule=\"nonzero\" node-id=\"1054\" stroke=\"none\" target-height=\"5.5\" target-width=\"23.400002\" target-x=\"14\" target-y=\"43\"/><path d=\"M 33.10 53.30 L 17.30 53.10 L 16.00 51.90 L 15.90 50.90 L 16.00 49.90 L 17.30 48.60 L 34.10 48.60 L 35.30 49.90 L 35.50 50.90 L 34.80 52.60 L 33.10 53.30 Z\" fill=\"url(#SVGID_166_)\" fill-rule=\"nonzero\" node-id=\"1056\" stroke=\"none\" target-height=\"4.700001\" target-width=\"19.6\" target-x=\"15.9\" target-y=\"48.6\"/><path d=\"M 79.70 26.50 L 73.50 40.50 L 84.60 36.60 L 79.70 26.50\" fill=\"url(#SVGID_167_)\" fill-rule=\"nonzero\" node-id=\"1058\" stroke=\"none\" target-height=\"14\" target-width=\"11.099998\" target-x=\"73.5\" target-y=\"26.5\"/><path d=\"M 32.20 82.40 L 18.70 82.30 L 17.90 81.10 L 17.90 75.10 L 18.30 74.10 L 19.20 73.80 L 32.70 73.90 L 33.50 75.10 L 33.50 81.10 L 33.10 82.10 L 32.20 82.40 Z\" fill=\"url(#SVGID_168_)\" fill-rule=\"nonzero\" node-id=\"1060\" stroke=\"none\" target-height=\"8.599998\" target-width=\"15.6\" target-x=\"17.9\" target-y=\"73.8\"/></svg>"
            });
            ExChangeGoodList.Add(new PromoteEventItem()
            {
                ItemCode = "0002815",
                ItemName = "怡思丁天然倍护婴儿二合一洗发沐浴露",
                ItemValue = "9.9",
                MainPic = "<svg height=\"100\" node-id=\"1\" sillyvg=\"true\" template-height=\"100\" template-width=\"100\" version=\"1.1\" viewBox=\"0 0 100 100\" width=\"100\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs node-id=\"917\"><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_151_\" node-id=\"844\" spreadMethod=\"pad\" x1=\"58.0978\" x2=\"58.1635\" y1=\"44.7995\" y2=\"62.6342\"><stop offset=\"0\" stop-color=\"#fffff8\"/><stop offset=\"0.9486\" stop-color=\"#fffbe3\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_152_\" node-id=\"848\" spreadMethod=\"pad\" x1=\"79.0717\" x2=\"78.9958\" y1=\"42.6926\" y2=\"85.7649\"><stop offset=\"0\" stop-color=\"#ebf6fd\"/><stop offset=\"0.7916\" stop-color=\"#c4dff8\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_153_\" node-id=\"852\" spreadMethod=\"pad\" x1=\"61.2715\" x2=\"61.2763\" y1=\"30.4902\" y2=\"44.4551\"><stop offset=\"0.1304\" stop-color=\"#b7dff9\"/><stop offset=\"0.6595\" stop-color=\"#95c5ec\"/><stop offset=\"0.8424\" stop-color=\"#88bbe7\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_154_\" node-id=\"857\" spreadMethod=\"pad\" x1=\"64.4871\" x2=\"64.628\" y1=\"25.4482\" y2=\"15.8301\"><stop offset=\"0\" stop-color=\"#cbe3f9\"/><stop offset=\"0.7916\" stop-color=\"#e9f4fd\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_157_\" node-id=\"867\" spreadMethod=\"pad\" x1=\"53.6808\" x2=\"53.6808\" y1=\"47.447\" y2=\"86.977\"><stop offset=\"0.1304\" stop-color=\"#97c7ec\"/><stop offset=\"0.7425\" stop-color=\"#7aafe1\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_160_\" node-id=\"877\" spreadMethod=\"pad\" x1=\"55.3621\" x2=\"55.3621\" y1=\"21.5554\" y2=\"13.4795\"><stop offset=\"0\" stop-color=\"#72a9de\"/><stop offset=\"0.5418\" stop-color=\"#6da6dd\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_161_\" node-id=\"881\" spreadMethod=\"pad\" x1=\"61.8731\" x2=\"61.8731\" y1=\"21.5554\" y2=\"13.4795\"><stop offset=\"0\" stop-color=\"#72a9de\"/><stop offset=\"0.5418\" stop-color=\"#6da6dd\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_162_\" node-id=\"885\" spreadMethod=\"pad\" x1=\"67.582\" x2=\"67.582\" y1=\"21.5554\" y2=\"13.4795\"><stop offset=\"0\" stop-color=\"#72a9de\"/><stop offset=\"0.5418\" stop-color=\"#6da6dd\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_163_\" node-id=\"889\" spreadMethod=\"pad\" x1=\"73.6919\" x2=\"73.6919\" y1=\"21.5554\" y2=\"13.4795\"><stop offset=\"0\" stop-color=\"#72a9de\"/><stop offset=\"0.5418\" stop-color=\"#6da6dd\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_164_\" node-id=\"893\" spreadMethod=\"pad\" x1=\"25.6422\" x2=\"25.6898\" y1=\"53.2609\" y2=\"86.6028\"><stop offset=\"0.1304\" stop-color=\"#fff0e6\"/><stop offset=\"0.4215\" stop-color=\"#ffdbc0\"/><stop offset=\"0.7425\" stop-color=\"#ffc291\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_165_\" node-id=\"898\" spreadMethod=\"pad\" x1=\"25.628\" x2=\"25.6995\" y1=\"42.7104\" y2=\"48.5795\"><stop offset=\"0.1304\" stop-color=\"#fff0e4\"/><stop offset=\"0.428\" stop-color=\"#ffdbbe\"/><stop offset=\"0.7425\" stop-color=\"#ffc291\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_166_\" node-id=\"903\" spreadMethod=\"pad\" x1=\"25.6878\" x2=\"25.6738\" y1=\"51.7196\" y2=\"48.9012\"><stop offset=\"0\" stop-color=\"#fffce5\"/><stop offset=\"0.4772\" stop-color=\"#fffdf1\"/><stop offset=\"1\" stop-color=\"#fffdf9\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_167_\" node-id=\"908\" spreadMethod=\"pad\" x1=\"76.6508\" x2=\"79.4813\" y1=\"29.5007\" y2=\"40.2463\"><stop offset=\"0.1657\" stop-color=\"#6ea7dd\"/><stop offset=\"0.7916\" stop-color=\"#76abdf\"/></linearGradient><linearGradient gradientUnits=\"objectBoundingBox\" id=\"SVGID_168_\" node-id=\"912\" spreadMethod=\"pad\" x1=\"25.6826\" x2=\"25.6847\" y1=\"73.7575\" y2=\"82.4364\"><stop offset=\"0.1719\" stop-color=\"#fffff7\"/><stop offset=\"0.9498\" stop-color=\"#fffbe3\"/></linearGradient><clipPath id=\"SVGID_156_\" node-id=\"864\"><path d=\"M 42.90 40.50 L 73.50 40.50 L 73.50 87.00 L 42.90 87.00 Z\" fill=\"#000000\" fill-rule=\"nonzero\" node-id=\"1012\" stroke=\"none\" target-height=\"46.5\" target-width=\"30.599998\" target-x=\"42.9\" target-y=\"40.5\"/></clipPath><clipPath id=\"SVGID_159_\" node-id=\"874\"><path d=\"M 75.70 15.00 L 77.30 15.30 L 78.60 16.20 L 79.40 17.40 L 79.70 19.00 L 79.70 26.50 L 49.30 26.50 L 49.30 19.00 L 49.60 17.40 L 50.50 16.20 L 51.80 15.30 L 53.30 15.00 L 75.70 15.00 Z\" fill=\"#000000\" fill-rule=\"nonzero\" node-id=\"1016\" stroke=\"none\" target-height=\"11.5\" target-width=\"30.399998\" target-x=\"49.3\" target-y=\"15\"/></clipPath></defs><path d=\"M 42.90 40.50 L 73.50 40.50 L 73.50 87.00 L 42.90 87.00 Z\" fill=\"url(#SVGID_151_)\" fill-rule=\"nonzero\" node-id=\"1022\" stroke=\"none\" target-height=\"46.5\" target-width=\"30.599998\" target-x=\"42.9\" target-y=\"40.5\"/><path d=\"M 84.60 82.90 L 73.50 87.00 L 73.50 40.60 L 84.60 36.60 Z\" fill=\"url(#SVGID_152_)\" fill-rule=\"nonzero\" node-id=\"1024\" stroke=\"none\" target-height=\"50.4\" target-width=\"11.099998\" target-x=\"73.5\" target-y=\"36.6\"/><path d=\"M 73.50 40.50 L 42.90 40.50 L 49.10 26.50 L 79.70 26.50 Z\" fill=\"url(#SVGID_153_)\" fill-rule=\"nonzero\" node-id=\"1026\" stroke=\"none\" target-height=\"14\" target-width=\"36.799995\" target-x=\"42.9\" target-y=\"26.5\"/><path d=\"M 75.70 15.00 L 77.30 15.30 L 78.60 16.20 L 79.40 17.40 L 79.70 19.00 L 79.70 26.50 L 49.30 26.50 L 49.30 19.00 L 49.60 17.40 L 50.50 16.20 L 51.80 15.30 L 53.30 15.00 L 75.70 15.00 Z\" fill=\"url(#SVGID_154_)\" fill-rule=\"nonzero\" node-id=\"1028\" stroke=\"none\" target-height=\"11.5\" target-width=\"30.399998\" target-x=\"49.3\" target-y=\"15\"/><g node-id=\"1062\"><g clip-path=\"url(#SVGID_156_)\" group-id=\"1\" node-id=\"1032\"><path d=\"M 73.50 50.00 L 73.50 87.00 L 34.50 86.60 L 33.90 50.60 L 41.90 50.60 L 41.90 66.80 L 42.10 67.80 L 42.70 68.60 L 43.80 69.20 L 46.40 69.10 L 47.80 67.80 L 48.00 55.40 L 48.20 54.40 L 49.50 53.00 L 50.50 52.80 L 51.50 53.00 L 52.90 54.40 L 53.10 62.80 L 53.30 63.80 L 54.60 65.10 L 55.60 65.40 L 56.60 65.10 L 58.00 63.80 L 58.20 59.30 L 58.90 57.50 L 60.70 56.80 L 62.50 57.50 L 63.30 59.30 L 63.50 71.00 L 64.80 72.30 L 65.80 72.50 L 67.60 71.80 L 68.40 70.00 L 68.60 49.00 L 69.90 47.70 L 70.90 47.50 L 72.70 48.20 L 73.50 50.00 Z\" fill=\"url(#SVGID_157_)\" fill-rule=\"nonzero\" group-id=\"1,3\" node-id=\"1034\" stroke=\"none\" target-height=\"39.5\" target-width=\"39.6\" target-x=\"33.9\" target-y=\"47.5\"/></g></g><g node-id=\"1063\"><g clip-path=\"url(#SVGID_159_)\" group-id=\"2\" node-id=\"1040\"><path d=\"M 55.40 15.00 L 55.40 20.10\" fill=\"none\" group-id=\"2,4\" node-id=\"1042\" stroke=\"url(#SVGID_160_)\" stroke-linecap=\"round\" stroke-width=\"3\" target-height=\"5.1000004\" target-width=\"0\" target-x=\"55.4\" target-y=\"15\"/><path d=\"M 61.90 15.00 L 61.90 20.10\" fill=\"none\" group-id=\"2,4\" node-id=\"1044\" stroke=\"url(#SVGID_161_)\" stroke-linecap=\"round\" stroke-width=\"3\" target-height=\"5.1000004\" target-width=\"0\" target-x=\"61.9\" target-y=\"15\"/><path d=\"M 67.60 15.00 L 67.60 20.10\" fill=\"none\" group-id=\"2,4\" node-id=\"1046\" stroke=\"url(#SVGID_162_)\" stroke-linecap=\"round\" stroke-width=\"3\" target-height=\"5.1000004\" target-width=\"0\" target-x=\"67.6\" target-y=\"15\"/><path d=\"M 73.70 15.00 L 73.70 20.10\" fill=\"none\" group-id=\"2,4\" node-id=\"1048\" stroke=\"url(#SVGID_163_)\" stroke-linecap=\"round\" stroke-width=\"3\" target-height=\"5.1000004\" target-width=\"0\" target-x=\"73.7\" target-y=\"15\"/></g></g><path d=\"M 34.50 86.60 L 16.80 86.60 L 14.70 86.00 L 13.20 84.40 L 12.60 82.30 L 13.10 73.50 L 17.60 53.30 L 33.80 53.30 L 38.30 73.50 L 38.80 82.30 L 38.60 83.40 L 37.50 85.30 L 35.70 86.40 L 34.50 86.60 Z\" fill=\"url(#SVGID_164_)\" fill-rule=\"nonzero\" node-id=\"1052\" stroke=\"none\" target-height=\"33.3\" target-width=\"26.199999\" target-x=\"12.6\" target-y=\"53.3\"/><path d=\"M 36.30 48.50 L 14.60 48.30 L 14.00 47.50 L 14.00 47.00 L 15.70 44.10 L 17.00 43.00 L 34.30 43.00 L 35.70 44.10 L 37.40 47.00 L 37.20 48.00 L 36.30 48.50 Z\" fill=\"url(#SVGID_165_)\" fill-rule=\"nonzero\" node-id=\"1054\" stroke=\"none\" target-height=\"5.5\" target-width=\"23.400002\" target-x=\"14\" target-y=\"43\"/><path d=\"M 33.10 53.30 L 17.30 53.10 L 16.00 51.90 L 15.90 50.90 L 16.00 49.90 L 17.30 48.60 L 34.10 48.60 L 35.30 49.90 L 35.50 50.90 L 34.80 52.60 L 33.10 53.30 Z\" fill=\"url(#SVGID_166_)\" fill-rule=\"nonzero\" node-id=\"1056\" stroke=\"none\" target-height=\"4.700001\" target-width=\"19.6\" target-x=\"15.9\" target-y=\"48.6\"/><path d=\"M 79.70 26.50 L 73.50 40.50 L 84.60 36.60 L 79.70 26.50\" fill=\"url(#SVGID_167_)\" fill-rule=\"nonzero\" node-id=\"1058\" stroke=\"none\" target-height=\"14\" target-width=\"11.099998\" target-x=\"73.5\" target-y=\"26.5\"/><path d=\"M 32.20 82.40 L 18.70 82.30 L 17.90 81.10 L 17.90 75.10 L 18.30 74.10 L 19.20 73.80 L 32.70 73.90 L 33.50 75.10 L 33.50 81.10 L 33.10 82.10 L 32.20 82.40 Z\" fill=\"url(#SVGID_168_)\" fill-rule=\"nonzero\" node-id=\"1060\" stroke=\"none\" target-height=\"8.599998\" target-width=\"15.6\" target-x=\"17.9\" target-y=\"73.8\"/></svg>"
            });
        }

        private void OpenExchangeGoodCommandImpl()
        {
            if (ExChangeGoodList.Count > 0)
            {
                if (ParentViewModel.PluList.Where(x => x.Tag == Plu.PluTag.ExChange).ToList().Count == 0)
                {
                    var view = Globals.PayForm;
                    var config = AntdUI.Modal.config(view, "超值整单特惠商品明细", new ExChangeView(ExChangeGoodList));
                    config.CloseIcon = true;
                    config.Mask = true;
                    config.BtnHeight = 0;
                    config.MaskClosable = true;
                    config.Keyboard = true;
                    AntdUI.Modal.open(config);
                }
                else
                {
                    MessageBox.Show("已累计达到限购数量，请取消结账并重新进入结账页面在进行换购", "提醒");
                }
            }
        }

        private void CalculateCommandImpl()
        { 
            
        }

        private void ResetCommandImpl()
        {
            // 清除一些信息
            Index = -1;
            HasPayList.Clear();
            TotalAmount = 0;
            ExchangeAmount = 0;
            RemainPayAmount = 0;
            HasPayedAmount = 0;
            PayAmount = 0;

            RoundAmount = 0;
            PayAmountString = string.Empty;
            SelectPay = null;
            // 删除所有换购的商品
            if (ExchangeCart.Count > 0)
            {
                List<Plu> goods = ParentViewModel.PluList.Where(g => ExchangeCart.Contains(g)).ToList();
                ParentViewModel.PluList.RemoveMany(goods);
                ExchangeCart.Clear();
            }
            ParentViewModel.SummaryCommand.Execute().Subscribe();
        }
    }
}
